<template>
  <view class="goods-item">
    <!-- 商品左侧图片区域 -->
    <view class="goods-item-left">
      <!-- 增加单选框组件 -->
      <radio v-if="showRadio" :checked="goods.goods_state" color="#C00000" @click="changeGoodsState"></radio>
      <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
    </view>
    <!-- 商品右侧信息区域 -->
    <view class="goods-item-right">
      <!-- 商品标题 -->
      <view class="goods-name">{{ goods.goods_name }}</view>
      <view class="goods-info-box">
        <!-- 商品价格 -->
        <view class="goods-price">￥{{ goods.goods_price | tofixed }}</view>
        <!-- 控制商品数量 -->
        <!-- 商品数量 -->
        <uni-number-box v-if="showNum" :min="1" :value="goods.goods_count" @change="changeGoodsCount"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'my-goods',
  props: {
    // 是否展示图片左侧的 radio
    showRadio: {
      type: Boolean,
      // 如果外界没有指定 show-radio 属性的值，则默认不展示 radio 组件
      default: false
    },
    // 是否展示价格右侧的 NumberBox 组件
    showNum: {
      type: Boolean,
      default: false
    },
    // 商品对象
    goods: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // 默认的空图片
      defaultPic: 'http://image4.suning.cn/uimg/b2c/newcatentries/0000000000-000000000606013705_1_400x400.jpg'
    };
  },
  methods: {
    // 点击单选框改变购物车商品的选中状态
    changeGoodsState() {
      // 触发父组件中改变购物车商品的自定义事件
      this.$emit('change-goods-state', { goods_id: this.goods.goods_id, goods_state: !this.goods.goods_state });
    },
    // 改变商品的数量
    changeGoodsCount(value) {
      this.$emit('change-goods-count', { goods_id: this.goods.goods_id, goods_count: +value });
    }
  }
};
</script>

<style lang="scss">
.goods-item {
  // 让 goods-item 项占满整个屏幕的宽度
  width: 750rpx;
  // 设置盒模型为 border-box
  box-sizing: border-box;
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;

  .goods-item-left {
    margin-right: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
    }

    .goods-info-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .goods-price {
        font-size: 16px;
        color: #c00000;
      }
    }
  }
}
</style>
